#ifndef MONTHCALENDARDISPLAY_H
#define MONTHCALENDARDISPLAY_H

#include <iostream>
#include <ctime>
#include <iomanip>
#include <windows.h>
#include <conio.h>
#include <vector>
#include <string>
#include "LunarSolarConverter.h"

using namespace std;

class fuction3 {
public:
    void show() {
        int year, month;
        cout << "请输入年份和月份（格式：YYYY MM）：";
        cin >> year >> month;
        Show(year, month);
    }

    void Show(int year, int month) {
        // 获取该月第一天是星期几
        tm first = {};
        first.tm_year = year - 1900;
        first.tm_mon = month - 1;
        first.tm_mday = 1;
        mktime(&first);
        int sweek = first.tm_wday;

        // 获取该月的天数
        tm nextMonth = first;
        nextMonth.tm_mon += 1;
        mktime(&nextMonth);
        int days = (mktime(&nextMonth) - mktime(&first)) / (60 * 60 * 24);
        // 获取节气列表
        vector<pair<int, string>> terms = jieqiriqi(year);

        // 打印标题
        cout << "\n\t\t" << year << "年" << month << "月\n";
        const char* week[] = { "日", "一", "二", "三", "四", "五", "六" };
        for (int i = 0; i < 7; i++) {
            if (i == 0 || i == 6) SetColor(12, 0); // 周末高亮
            cout << setw(10) << week[i];
            SetColor(7, 0);
        }
        //创建一条分割线
        cout << "\n" << string(10 * 7, '-') << "\n";

        // 打印前面的空格
        int position = 0;
        for (int i = 0; i < sweek; i++) {
            cout << setw(10) << " ";
            position++;
        }

        // 正式打印每一天
        for (int day = 1; day <= days; day++) {
            if (position % 7 == 0 && position != 0) cout << "\n";

            if (position % 7 == 0 || position % 7 == 6) SetColor(12, 0);
            cout << setw(3) << day << " ";

            // 判断是否是节气
            string jieqi = shibushijieqi(terms, month, day);
            if (jieqi != "") {
                cout << jieqi;
            } else {
                // 显示农历日
                Solar s;
                s.solarYear = year;
                s.solarMonth = month;
                s.solarDay = day;
                Lunar l = SolarToLunar(s);
                cout << dayname(l.lunarDay);
            }

            SetColor(7, 0);
            cout << setw(3) << " ";
            position++;
        }

        cout << "\n\n继续......" << endl;
        _getch();
    }

private:
    string dayname(int day) {
        const char* names[] = {
            "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
            "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
            "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"
        };
        if (day >= 1 && day <= 30) return names[day - 1];
    }

    void SetColor(int fg, int bg) {
        HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(h, (WORD)((bg << 4) | fg));
    }

    string shibushijieqi(vector<pair<int, string>> list, int month, int day) {
        int key = month * 100 + day;
        for (int i = 0; i < list.size(); i++) {
            if (list[i].first == key) return list[i].second;
        }
        return "";
    }

    vector<pair<int, string>> jieqiriqi(int year) {
        vector<pair<int, string>> terms;
        terms.push_back(make_pair(106, "小寒"));
        terms.push_back(make_pair(120, "大寒"));
        terms.push_back(make_pair(204, "立春"));
        terms.push_back(make_pair(219, "雨水"));
        terms.push_back(make_pair(305, "惊蛰"));
        terms.push_back(make_pair(320, "春分"));
        terms.push_back(make_pair(404, "清明"));
        terms.push_back(make_pair(419, "谷雨"));
        terms.push_back(make_pair(505, "立夏"));
        terms.push_back(make_pair(520, "小满"));
        terms.push_back(make_pair(605, "芒种"));
        terms.push_back(make_pair(621, "夏至"));
        terms.push_back(make_pair(707, "小暑"));
        terms.push_back(make_pair(722, "大暑"));
        terms.push_back(make_pair(807, "立秋"));
        terms.push_back(make_pair(823, "处暑"));
        terms.push_back(make_pair(907, "白露"));
        terms.push_back(make_pair(922, "秋分"));
        terms.push_back(make_pair(1008, "寒露"));
        terms.push_back(make_pair(1023, "霜降"));
        terms.push_back(make_pair(1107, "立冬"));
        terms.push_back(make_pair(1122, "小雪"));
        terms.push_back(make_pair(1207, "大雪"));
        terms.push_back(make_pair(1221, "冬至"));
        return terms;
    }
};
#endif
